// component/oavlLogin/ovalLogin.js
var util = require('../../we7/resource/js/util.js')
let WebIM = require("../../utils/webim")["default"];
var app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  
  /**
   * 组件的初始数据
   */
  data: {
    maskshow: true,
    maskCont: true,
    firstmask:false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    goOn:function(){
      this.triggerEvent('flagEvent', {});
    },
    quxiao:function(){
      this.setData({
        maskshow: false,
        maskCont: false,
        firstmask: true,
      })
    },
    tanchu:function(){
      this.setData({
        maskshow: true,
        maskCont: true,
        firstmask: false,
      })
    },
    // 授权登录
    bindGetUserInfo: function (t) {
      let that = this;

      if (t.detail.userInfo) {
        app.globalData.wxInfo = t.detail.userInfo; //用户微信信息
        //获取登录信息（sessionkey，openid）
        that.getLoginInfo();
      } else {
        console.log('用户点击了“拒绝授权”')
      }
    },
    //获取登陆信息
    getLoginInfo() {
      let that = this;
      wx.login({
        success(res) {
          console.log(res)
          if (res.code) {
            app.util.request({
              url: "entry/wxapp/Authorize.getuid",
              data: {
                code: res.code
              },
              success: function (e) {
                console.log(e)
                app.globalData.sessionInfo = e.data; //用户sessionkey & openid
                that.isLogin();
              }
            })
          }
        }
      })
    },
    isLogin() {
      let that = this;
      var openid = app.globalData.sessionInfo.openid
      var nickName = app.globalData.wxInfo.nickName
      var avatarUrl = app.globalData.wxInfo.avatarUrl
      app.util.request({
        url: "entry/wxapp/Authorize.tymember",
        data: {
          u_name: nickName,
          u_thumb: avatarUrl,
          openid: openid
        },
        success: function (t) {
          console.log(t)
          //查询用户是否已注册过
          app.util.request({
            url: "entry/wxapp/Authorize.ifregister",
            data: {
              openid: openid
            },
            success: function (resing) {
              console.log(resing)
              if (resing.data == "" || resing.data == undefined || resing.data == 'undefined' || !resing.data) {
                app.util.request({
                  'url': 'entry/wxapp/Authorize.updateadmin',
                  data: {
                    openid: openid
                  },
                  success: function (res) {
                    console.log(res)
                    var username = res.data
                    var options = {
                      apiUrl: WebIM.config.apiURL,
                      username: username,
                      password: '123',
                      nickname: "",
                      appKey: WebIM.config.appkey,
                      success: function (res) {
                        console.log('res', res)
                        if (res.statusCode == "200") {
                          console.log('注册成功');
                          //更新用户环信账号
                          var data = {
                            apiUrl: WebIM.config.apiURL,
                            user: username,
                            pwd: that.data.password,
                            grant_type: "password",
                            appKey: WebIM.config.appkey
                          };
                          wx.setStorage({
                            key: "myUsername",
                            data: username
                          });
                        }
                      },
                      error: function (res) {
                        console.log('res', res)
                        if (res.statusCode !== "200") {
                          if (res.statusCode == '400' && res.data.error == 'illegal_argument') {
                            console.log('用户名非法!')
                          }
                          console.log('用户名已被占用!')
                        }
                      }
                    };
                    WebIM.utils.registerUser(options);
                  }
                })
              } else {
                app.util.request({
                  url: "entry/wxapp/Authorize.ifregister",
                  data: {
                    openid: openid
                  },
                  success: function (res) {
                    var username = res.data
                    wx.setStorage({
                      key: "myUsername",
                      data: username
                    });
                  }
                })

              }
            }
          })

          app.globalData.userInfo = t.data; //登录成功返回用户信息
          // app.globalData.maskshow = false;
          // app.globalData.maskCont=false;
          wx.setStorageSync("openid", openid);
          wx.setStorageSync("userInfo", t.data)
          that.setData({
            maskshow: false,
            maskCont: false,
            firstmask: false,
          })
          // wx.setStorageSync('maskshow', that.data.maskshow)
          // wx.setStorageSync('maskCont', that.data.maskCont)
          that.triggerEvent('flagEvent', {}); //登录成功后继续执行
        }
      })
    },

  }
})
